package com.rednote.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rednote.common.Result;
import com.rednote.entity.Comments;
import com.rednote.entity.dto.CommentDTO;
import com.rednote.entity.dto.PostDTO;
import com.rednote.entity.vo.CommentVO;
import com.rednote.service.CommentsService;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import org.springframework.web.multipart.MultipartFile;

/**
 * 帖子评论表(Comments)控制层
 *
 * @author yao suxuan
 * @since 2025-05-24 18:10:30
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Resource
    private CommentsService commentsService;



    /**
     * 回复评论
     */
    @PostMapping("/comments")
    public Result<?> replyComment(@ModelAttribute CommentDTO commentDTO) {
        try {
            commentsService.createComment(commentDTO.getId(), commentDTO.getContent(),commentDTO.getImages(),false);
            return Result.success("评论成功");
        }
        catch (Exception e) {
            return Result.fail("评论失败: " + e.getMessage());
        }
    }


    /**
     * 删除评论
     * @param id 评论ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}/comments")
    public Result<?> deleteComment(@PathVariable Integer id) {
        try {
            commentsService.removeCommentById(id);
            return Result.success("删除评论成功");

        } catch (Exception e) {
            return Result.fail("删除评论失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页大小
     * @return 查询结果
     */
    @GetMapping
    public Page<Comments> list(@RequestParam(defaultValue = "1") Integer page, 
                                        @RequestParam(defaultValue = "10") Integer size) {
        return commentsService.page(new Page<>(page, size));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Comments getById(@PathVariable Long id) {
        return commentsService.getById(id);
    }
    
    /**
     * 新增数据
     *
     * @param comments 实体对象
     * @return 新增结果
     */
    @PostMapping
    public boolean save(@RequestBody Comments comments) {
        return commentsService.save(comments);
    }

    /**
     * 修改数据
     *
     * @param comments 实体对象
     * @return 修改结果
     */
    @PutMapping
    public boolean update(@RequestBody Comments comments) {
        return commentsService.updateById(comments);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public boolean deleteById(@PathVariable Long id) {
        return commentsService.removeById(id);
    }
}
